package com.joke.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.joke.auto.JokeFetchQuartz;

/**
 * 这个类作为异步任务，用来随时执行抓取笑话的方法。人工调用，方便测试。
 * @author zhaozongyi
 * @date 2018-3-18
 */
public class FetchJokeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 日志输出
	private static final Logger logger = LoggerFactory.getLogger(FetchJokeServlet.class);	/**
	 * Constructor of the object.
	 */
	public FetchJokeServlet() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		JokeFetchQuartz jokeFetchQuartz = new JokeFetchQuartz();
		String exflag = "Success";
		String exdesc = "执行完毕";
		try {
			logger.info("开始调用FetchJokeServlet===>>"+super.getClass().getSimpleName()+"<<");
			exflag = jokeFetchQuartz.fetch();
		} catch (Exception e) {
			exflag = e.getMessage();
			exdesc = "执行失败";
		}finally{
			// 回传异步任务系统信息
			OutputStream outputStream = response.getOutputStream();
			outputStream.write(exflag.getBytes());
			outputStream.flush();
			logger.info("调用FetchJokeServlet===>>"+super.getClass().getSimpleName()+exdesc+"<<");
		}

	}

	public void init() throws ServletException {
		
	}
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy();
	}

}
